//
//  BBSMessageViewController.m
//  MilestoneProject
//
//  Created by 张育铭 on 2019/10/22.
//  Copyright © 2019 mac. All rights reserved.
//

#import "BBSMessageViewController.h"
#import "ToolControl.h"
#import "BBSMessageViewCell.h"
#import "CJTableViewModel.h"
#import "BBSMessageViewController.h"

@interface BBSMessageViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView  *tableView;

/// 热门
@property (nonatomic,strong) NSMutableArray *hotDataArray;
/// 回复
@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation BBSMessageViewController{
    CGFloat theHeight;
    /// 页码
    NSInteger startIndex;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    /// 自定义导航栏
    [self setNavBarTitle:_model.theTitleItem];
    [self setNavBarRightBtn:[GHOCNavBarView createImgNaviBarBtnByImgNormal:@"luntanshare" imgHighlight:nil target:self action:@selector(rightBarButtonClick:)]];
    
    theHeight = theIsIphoneX? 83: 49;
    [self.view addSubview:self.tableView];
    [self.view addSubview:[self footView]];

    [self downLodingDate];
}

- (void)changeMessageBtnClick {
    
}

- (void)rightBarButtonClick:(UIButton *)sender {
    
}

/// 论坛详情
- (void)downLodingDate {
    [[HTTPManager sharedInstance] GetWithPath:@"/frontend/news/forum/detail" parametersDict:@{@"topic_id":_model.theId} sucessBlock:^(NSURLSessionDataTask *operation, id responseObject) {
        if ([responseObject[@"code"] integerValue] == 0) {
            [self.dataArray    removeAllObjects];
            [self.hotDataArray removeAllObjects];

            CJTableViewModel *model = [[CJTableViewModel alloc]initWithDict:responseObject[@"data"]];
            [self.dataArray addObject:model];
            
            for (NSDictionary *dic in responseObject[@"data"][@"replies"]) {
                CJTableViewModel *repliesModel = [[CJTableViewModel alloc]initReplaceWithDict:dic];
                [self.hotDataArray addObject:repliesModel];
            }
            [self.tableView reloadData];
        }
    } failtBlock:^(NSURLSessionDataTask *operation, id error) {
    }];
}
/// 设置头视图
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return .1;
    }
    return 50;
}
/// 头视图风格
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view          = [UIView new];
//    view.backgroundColor  = RGBCOLOR(244, 244, 244);
    if (section == 1) {
        UILabel *label = [ToolControl creaImagetTitleLabelWithFrame:CGRectMake(40, 0, SCREEN_WIDTH, 49) theImageStr:@"main-chat" theImageWidth:20 theText:@" 全部回复"];
        [view addSubview:label];
        
        UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
        lineImage.backgroundColor = [UIColor grayColor];
        [view addSubview:lineImage];
    }
    return view;
}
/// 分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

/// 对应的数据量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.dataArray.count;
    }
    return self.hotDataArray.count;
}
/// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return [self.tableView cellHeightForIndexPath:indexPath model:self.dataArray[indexPath.row] keyPath:@"model" cellClass:[BBSMessageViewCell class] contentViewWidth:SCREEN_WIDTH];
    }
    return [self.tableView cellHeightForIndexPath:indexPath model:self.hotDataArray[indexPath.row] keyPath:@"model" cellClass:[BBSMessageRevertViewCell class] contentViewWidth:SCREEN_WIDTH];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .1;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        BBSMessageViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[BBSMessageViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
        cell.model = self.dataArray[indexPath.row];
        cell.moreView.shareAndpraiseBtnBlock = ^(NSInteger tag) {
            if (tag == 1002) {
//                NSDictionary *dic = @{@"person.id":NSUSERDEFAULTS[@"id"],@"sectionTimeline.id":cell.model.connected_uid};
//                [[HelperManager sharedInstance]getSectionTimelineLikeDict:dic DateSuccess:^(NSURLSessionDataTask *operation, id responseObject) {
//                    if ([responseObject[@"body"][@"isLiked"]integerValue] == 0) {
//                        cell.model.likecount ++;
//                        cell.model.isDianZan = @"123";
//                        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//
//                        //点赞
//                        [[HelperManager sharedInstance]getSectionLikeAddDict:dic DateSuccess:^(NSURLSessionDataTask *operation, id responseObject) {
//                            NSLog(@"%@",responseObject);
//
//                        } failure:^(NSURLSessionDataTask *operation, NSError *error) {
//                        }];
//                    }
//                    else {
//                        cell.model.likecount --;
//                        cell.model.isDianZan = @"";
//
//                        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//
//                        //取消点赞
//                        [[HelperManager sharedInstance]getSectionLikeDelDict:@{@"id":responseObject[@"body"][@"id"]} DateSuccess:^(NSURLSessionDataTask *operation, id responseObject) {
//                            NSLog(@"%@",responseObject);
//                        } failure:^(NSURLSessionDataTask *operation, NSError *error) {
//                        }];
//                    }
//                } failure:^(NSURLSessionDataTask *operation, NSError *error) {
//                }];
            }
        };
        cell.coreTextClick = ^{
            [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
        };
        return cell;
    }
    else {
        BBSMessageRevertViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BBSMessageRevertViewCell"];
        if (!cell) {
            cell = [[BBSMessageRevertViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BBSMessageRevertViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
        cell.model = self.hotDataArray[indexPath.row];
        return cell;
    }
}

- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, SCREEN_TabbarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-SCREEN_TabbarHeight-theHeight)style:(UITableViewStyleGrouped)];
        _tableView.delegate        = self;
        _tableView.dataSource      = self;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorColor  = [UIColor clearColor];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
    }
    return _tableView;
}
- (NSMutableArray*)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray*)hotDataArray {
    if (_hotDataArray == nil) {
        _hotDataArray = [NSMutableArray array];
    }
    return _hotDataArray;
}
// 回复框UI
- (UIView*)footView {
    UIView *headBackView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-theHeight, SCREEN_WIDTH, theHeight)];
                                                                    
    UIButton * changeMessageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [changeMessageBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:17]];
    [changeMessageBtn setTitle:@" 回复" forState:UIControlStateNormal];
    [changeMessageBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    changeMessageBtn.frame = CGRectMake(0, 0, SCREEN_WIDTH, 49);
    [changeMessageBtn setImage:[UIImage imageNamed:@"bbs-reply-lt"] forState:(UIControlStateNormal)];
    [changeMessageBtn addTarget:self action:@selector(changeMessageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [headBackView addSubview:changeMessageBtn];
  
    return headBackView;
}
@end
